﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<link rel="stylesheet" type="text/css" media="screen" href="css/jquery-ui-1.8.1.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="css/ui.jqgrid.css" />
<script src="js/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-cn.js" type="text/javascript"></script>
<script src="js/json2.js" type="text/javascript"></script>
<script src="js/xing.js" type="text/javascript"></script>
<script type="text/javascript">
	$.jgrid.no_legacy_api = true;
	$.jgrid.useJSON = true;
</script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
var firstLoad = true;
var udistrict, usid;

function searchCommunity() {
	var district = $("#sf_district").val();
	var sid = $("#sf_street").val();
	
	if (this.id == "sf_submit")
		jQuery("#list1").jqGrid('setGridParam',{url:"base?action=2&district=" + district + "&sid=" + sid}).trigger("reloadGrid");
	else
		$("#df").attr("src", "base?oper=excel&action=2&district=" + district + "&sid=" + sid);
}

function initStreetSelect(district) {
	$.get("base?action=11&district=" + district,function (data, textStatus) {
		var ops = data;
		var len = ops.length;
		$("#sid").html(ops.substring(38, len - 9));
	});
};

function sfDistrictChange() {
	var district = $("#sf_district").val();
	
	$.get("base?action=11&district=" + district,function (data, textStatus) {
		var ops = data;
		var len = ops.length;
		$("#sf_street").html("<option value='0'>请选择乡镇街道</option>" + ops.substring(38, len - 9));
	});
}

function setStreet() {
  var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
  var sname = jQuery("#list1").jqGrid('getCell', gr, 4);
	$('#sid option').each(function() {
		if ($(this).attr('text') == sname)
			$('#sid').val($(this).attr('value'));
	});
};

function setGridButton() {
	if ((!firstLoad) || (udistrict == "0") || (usid != "0"))
		return;
		
	jQuery("#list1").jqGrid('navGrid','#gridpager1',{edit:false,add:false,del:false,refresh:false,search:false})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"添加", 
   		buttonicon:"ui-icon-plus", 
   		onClickButton: function(){
				jQuery("#list1").jqGrid('editGridRow','new',{
						addCaption:"添加一个村社区",
						bSubmit:"保存",bCancel:"取消",modal: true, 
						closeAfterAdd:true, 
						reloadAfterSubmit:true,
						beforeShowForm:function(formid){
							$('#cid').attr("readonly",false);
							initStreetSelect(udistrict);
						},
						afterSubmit : function(r, postdata) {
			  					var data = eval('(' + r.responseText + ')');
			  					switch (data.code) {
			  						case 0: return [true, data.message]; break;
			  						case 999: return [false, "只能添加本县区乡镇街道社区信息"]; break;
			  						case 30000: return [false, "添加失败，编码不能重复！"]; break;
			  						default: return [false, data.message];
			  					}
			  				}
				}); 
			}
		})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"更改", 
   		buttonicon:"ui-icon-wrench", 
   		onClickButton: function(){
				var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
				if( gr != null ) {
					var district = jQuery("#list1").jqGrid('getCell', gr, 2);
					if (district != udistrict) {
				  	showTips("只能更改本县区社区");
				  	return;
				  }
				  
				  jQuery("#list1").jqGrid('editGridRow',gr,{
				  	editCaption:"更改村社区信息",
				  	bSubmit:"保存",bCancel:"取消",modal: true,
				  	closeAfterEdit:true,
				  	viewPagerButtons:false, 
				  	reloadAfterSubmit:true,
				  	beforeShowForm:function(formid){
				  		$('#cid').attr("readonly",true);
				  		initStreetSelect(district);
				  	  setTimeout(setStreet,300);
				  	},
						afterSubmit : function(r, postdata) {
			  					var data = eval('(' + r.responseText + ')');
			  					switch (data.code) {
			  						case 0: return [true, data.message]; break;
			  						case 999: return [false, "只能修改本县区街道社区信息"]; break;
			  						default: return [false, data.message];
			  					}
			  		}
				  });
				} else
					showTips("请选择要更改的村社区");
			}
		})
		.jqGrid('navButtonAdd','#gridpager1',{
   		caption:"删除", 
   		buttonicon:"ui-icon-close", 
   		onClickButton: function() {
				var gr = jQuery("#list1").jqGrid('getGridParam','selrow');
				if( gr != null ) {
					var district = jQuery("#list1").jqGrid('getCell', gr, 2);
					if (district != udistrict) {
				  	showTips("只能删除本县区的村社区");
				  	return;
				  }
				  
				  jQuery("#list1").jqGrid('delGridRow', gr, {
				  	caption:"确定要删除这个村社区吗?", 
				  	msg:"确定要删除这个村社区？",
				  	bSubmit:"删除",bCancel:"取消",modal: true,
				  	closeAfterEdit:true, 
				  	reloadAfterSubmit:true,
				  	afterSubmit : function(r, postdata) {
			  					var data = eval('(' + r.responseText + ')');
			  					switch (data.code) {
			  						case 0: return [true, data.message]; break;
			  						default: return [false, data.message];
			  					}
			  		}
			  	});
				} else
					showTips("请选择要删除的村社区");
			}
		});
		
	firstLoad = false;
};

function setGrid() {
	jQuery("#list1").jqGrid({ 
			url:'base?action=2',
			datatype: "json",
			hidegrid: "false",
			colNames:['村社区编码','村社区名称','所属县区','所属乡镇街道','所属乡镇街道','电话','办公地址'], 
			colModel:[
					{name:'cid', width:120,align:'right',sortable:false,editable:true,editoptions:{size:12,maxlength:7},editrules:{required:true,number:true}}, 
					{name:'cname', width:200,align:'right', sortable:false,editable:true,editoptions:{size:15,maxlength:15},editrules:{required:true}},
					{name:'district',width:160,align:'right', sortable:false, formatter:'select', editoptions:{value:"1:市辖区;2:桥东区;3:桥西区;4:高新区;5:宣化区;6:下花园区"}},
					{name:'sid', width:200,align:'right', sortable:false,editable: true,edittype:'select',formatter:'select',hidden:true, editoptions:{value:"0:请选择街道"},editrules:{edithidden:true}},
					{name:'sname', width:160,align:'right', sortable:false,editoptions:{size:18,maxlength:11}},
					{name:'tel',width:170,align:'right', sortable:false,editable:true,editoptions:{size:16,maxlength:11},editrules:{required:true,number:true}},
					{name:'address', width:290,align:'right', sortable:false,editable: true, edittype:"textarea",editoptions:{rows:"2",cols:"18"},editrules:{required:true}}
			], 
			viewrecords: true,
			rowNum:20,
			pager: '#gridpager1',
			editurl:'base?action=2',
			caption:"村社区管理",
			height: 460,
			gridComplete: function() {
				udistrict = jQuery("#list1").jqGrid('getGridParam', 'userData')["udistrict"];
				usid = jQuery("#list1").jqGrid('getGridParam', 'userData')["usid"];
				setGridButton();
			}
  })
}; 
	
	$(document).ready(function(){
		setGrid();
		$("#sf_submit").click(searchCommunity);
		$("#sf_excel").click(searchCommunity);
	});
</script>
<body style="background-color:#d9f1cc;">
	<table>
	<tr>
		<td>选择县区</td>
		<td><select id="sf_district" name="sf_district" onChange="sfDistrictChange()">
		                		<option value="0">全  市</option>
		                		<option value="1">市辖区</option>
		                		<option value="2">桥东区</option>
		                		<option value="3">桥西区</option>
		                		<option value="4">高新区</option>
		                		<option value="5">宣化区</option>
		                		<option value="6">下花园区</option>
		                	</select></td>
		<td>乡镇街道办事处 </td>
	             <td><select id="sf_street" name="sf_street">
	                		<option value='0'>选择乡镇街道</option>
	                 </select></td>
		</td>
		<td width="100" align="right"><input type="button" id="sf_submit" value="查 询"></td>
		<td><input type="button" id="sf_excel" value="导 出"></td>
	</tr>
	</table>
				<iframe id="df"  name="df" src="" width=0 height=0 frameborder="0" ></iframe>
				<div id="RightPane">
					<div class="innertube">
					<table id="list1"></table>
					<div id="gridpager1"></div>
					</div>
				</div>

</body>
